
#include <stdint.h>
#include <string.h>
#include "board.h"

#define TICK_HOOK_MAX 4
static void (*tick_hook[TICK_HOOK_MAX])(void);

int ev_tick_hook_register(void (*hook)(void))
{
    int ret = -1;
    if (hook)
    {
        uint32_t level = driv_disable_irq();
        for (int i = 0; i < TICK_HOOK_MAX; i ++)
        {
            if (!tick_hook[i])
            {
                tick_hook[i] = hook;
                ret = 0;
                break;
            }
        }
        driv_enable_irq(level);
    }
    return ret;
}

int ev_tick_hook_unregister(void (*hook)(void))
{
    int ret = -1;
    if (hook)
    {
        uint32_t level = driv_disable_irq();
        for (int i = 0; i < TICK_HOOK_MAX; i ++)
        {
            if (tick_hook[i] == hook)
            {
                tick_hook[i] = NULL;
                ret = 0;
                break;
            }
        }
        driv_enable_irq(level);
    }
    return ret;
}

/* should be called int critical area */
void ev_tick_hook_schedule(void)
{
    for (int i = 0; i < TICK_HOOK_MAX; i ++)
    {
        if (tick_hook[i])
        {
            tick_hook[i]();
        }
    }
}
